/*
 * Created on Jul 12, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.herohead;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

/**
 * This classes contain support classes for email, etc
 * 
 * @author david-1
 *
 */
public class Support {

	static Logger logger = Logger.getLogger(Support.class);

	/**
	 * read file and store it in byte form
	 * 
	 * @param file file to read
	 * @return file in byte form
	 * @throws IOException
	 */

	//	############################################################################

	/**
	 * 
	 * @param file
	 * @return bytes
	 * @throws IOException
	 */

	@SuppressWarnings("resource")
	public static byte[] getBytesFromFile(File file) throws IOException {
		InputStream is = new FileInputStream(file);

		// Get the size of the file
		long length = file.length();

		// You cannot create an array using a long type.
		// It needs to be an int type.
		// Before converting to an int type, check
		// to ensure that file is not larger than Integer.MAX_VALUE.
		if (length > Integer.MAX_VALUE) {
			// File is too large
		}

		// Create the byte array to hold the data
		byte[] bytes = new byte[(int)length];

		// Read in the bytes
		int offset = 0;
		int numRead = 0;
		while (offset < bytes.length
				&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
			offset += numRead;
		}

		// Ensure all the bytes have been read in
		if (offset < bytes.length) {
			throw new IOException("Could not completely read file "+file.getName());
		}

		// Close the input stream and return bytes
		is.close();
		return bytes;
	}
	/**
	 * check for a single file. If it exits it will return true
	 * 
	 * @param file
	 * @return true or false
	 */
	public boolean check4File (String file) {

		File dir = new File(file);

		System.out.println("here is the file that is being looked for " + file);

		if (dir.exists()) {
			return true;
		} else
			return false;
	}

	/**
	 * 
	 * @param directory
	 * @param filefilter
	 * @return String array of files
	 * @throws Error
	 */

	public String[] check4FileEndWith (String directory, final String filefilter) throws Error {

		File dir = new File(directory);
		String fileList[];

		FilenameFilter filter = new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return !name.endsWith(filefilter);
			}
		};
		fileList = dir.list(filter);

		if (fileList.length > 0) {
			return fileList;
		} else {
			throw new Error("No Files Found");
		}
	}

	/**
	 * 
	 * @param directory
	 * @return String array of file list
	 * @throws Error
	 */

	public String[] check4Files (String directory) throws Error {

		File dir = new File(directory);
		String fileList[];
		String filesOnly[] = new String[1000];
		fileList = dir.list();
		int aCnt = 0;

		for (int i = 0; i < fileList.length; i++) {
			File fle = new File(directory + "/" + fileList[i]);
			if (fle.isFile()) {
				filesOnly[aCnt] = fileList[i];
				aCnt++;
			}
		}

		String[] filesOnly1 = new String[aCnt];

		System.arraycopy(filesOnly,0,filesOnly1,0,aCnt);

		if (filesOnly1.length > 0) {
			return filesOnly1;
		} else {
			throw new Error("No Files Found");
		}
	}

	/**
	 * 
	 * @param directory
	 * @param filefilter
	 * @return String array of files
	 * @throws Error
	 */
	public String[] check4FileStartsWith (String directory, final String filefilter) throws Error {

		File dir = new File(directory);
		String fileList[];

		FilenameFilter filter = new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return !name.startsWith(filefilter);
			}
		};
		fileList = dir.list(filter);

		if (fileList.length > 0) {
			return fileList;
		} else {
			throw new Error("No Files Found");
		}
	}
	/**
	 * Send email
	 * @param to "craig.david@mt.com" or can be "Craig <craig.david@mt.com>"
	 * @param from same as the to
	 * @param subject String subject
	 * @param theMessage String message to include in the body
	 */

	//	############################################################################
	public void sendMail(String to, String from, String subject, String theMessage){
		//	############################################################################

		String host = "smtp.mtus.mtnet";

		//			 Get system properties
		Properties props = System.getProperties();

		//			 Setup mail server
		props.put("mail.smtp.host", host);


		//			 Get session
		Session session = Session.getDefaultInstance(props, null);

		//			 Define message
		MimeMessage message = new MimeMessage(session);

		try {

			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			message.setSubject(subject);
			message.setText(theMessage);

			//			 Send message
			Transport.send(message);
		} catch (Exception e)
		{
			e.printStackTrace();
		}

	}

	/**
	 * send Email with an Attachment and also with text.
	 * this version can only attach a single file
	 * 
	 * @param to "craig.david@mt.com" or can be "Craig <craig.david@mt.com>"
	 * @param from same as to
	 * @param subject String subject
	 * @param theMessage String message to include in the body
	 * @param fle filename to attach
	 */
	//	############################################################################
	public void sendMailAtt(String to, String from, String subject, String theMessage, String fle){
		//	############################################################################

		String host = "smtp.mtus.mtnet";

		//			 Get system properties
		Properties props = System.getProperties();

		//			 Setup mail server
		props.put("mail.smtp.host", host);


		//			 Get session
		Session session = Session.getDefaultInstance(props, null);

		//			 Define message
		MimeMessage message = new MimeMessage(session);

		try {

			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			message.setSubject(subject);

			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(theMessage);

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);

			messageBodyPart = new MimeBodyPart();
			DataSource source = new FileDataSource(fle);
			messageBodyPart.setDataHandler(new DataHandler(source));
			messageBodyPart.setFileName(fle);
			multipart.addBodyPart(messageBodyPart);

			message.setContent(multipart);

			//			 Send message
			Transport.send(message);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Change directory slashes
	 * 
	 * @param str
	 * @return converted string
	 */


}
